/*     */ package com.zimbra.cs.util;
/*     */ 
/*     */ import com.zimbra.common.calendar.WellKnownTimeZones;
/*     */ import com.zimbra.common.lmtp.SmtpToLmtp;
/*     */ import com.zimbra.common.localconfig.ConfigException;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.localconfig.LocalConfig;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.SoapTransport;
/*     */ import com.zimbra.common.util.FileUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraHttpConnectionManager;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.AttributeManager;
/*     */ import com.zimbra.cs.account.AuthTokenRegistry;
/*     */ import com.zimbra.cs.account.AutoProvisionThread;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.ExternalAccountManagerTask;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.RightManager;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.db.DbPool;
/*     */ import com.zimbra.cs.db.DbPool.DbConnection;
/*     */ import com.zimbra.cs.db.DbSession;
/*     */ import com.zimbra.cs.db.Versions;
/*     */ import com.zimbra.cs.extension.ExtensionUtil;
/*     */ import com.zimbra.cs.iochannel.MessageChannel;
/*     */ import com.zimbra.cs.mailbox.MailboxIndex;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.PurgeThread;
/*     */ import com.zimbra.cs.mailbox.ScheduledTaskManager;
/*     */ import com.zimbra.cs.mailbox.acl.AclPushTask;
/*     */ import com.zimbra.cs.memcached.MemcachedConnector;
/*     */ import com.zimbra.cs.redolog.RedoLogProvider;
/*     */ import com.zimbra.cs.server.ServerManager;
/*     */ import com.zimbra.cs.servlet.FirstServlet;
/*     */ import com.zimbra.cs.session.SessionCache;
/*     */ import com.zimbra.cs.session.WaitSetMgr;
/*     */ import com.zimbra.cs.stats.ZimbraPerf;
/*     */ import com.zimbra.cs.store.StoreManager;
/*     */ import com.zimbra.cs.zookeeper.CuratorManager;
/*     */ import com.zimbra.cs.zookeeper.Service;
/*     */ import com.zimbra.znative.Util;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.security.Security;
/*     */ import java.util.Timer;
/*     */ import org.apache.mina.core.buffer.IoBuffer;
/*     */ import org.dom4j.DocumentException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class Zimbra
/*     */ {
/*  75 */   private static boolean sInited = false;
/*  76 */   private static boolean sIsMailboxd = false;
/*  77 */   private static String alwaysOnClusterId = null;
/*  78 */   private static Service service = null;
/*     */   
/*     */   private static final String HEAP_DUMP_JAVA_OPTION = "-xx:heapdumppath=";
/*     */   
/*     */ 
/*     */   private static void setSystemProperties()
/*     */   {
/*  85 */     System.setProperty("mail.mime.decodetext.strict", "false");
/*  86 */     System.setProperty("mail.mime.encodefilename", "true");
/*  87 */     System.setProperty("mail.mime.charset", "utf-8");
/*  88 */     System.setProperty("mail.mime.base64.ignoreerrors", "true");
/*  89 */     System.setProperty("mail.mime.ignoremultipartencoding", "false");
/*  90 */     System.setProperty("mail.mime.multipart.allowempty", "true");
/*     */   }
/*     */   
/*     */   private static void validateJavaOptions()
/*     */     throws ServiceException
/*     */   {
/*  96 */     String options = LC.mailboxd_java_options.value();
/*  97 */     if ((options != null) && (options.toLowerCase().indexOf("-xx:heapdumppath=") > -1)) {
/*  98 */       int start = options.toLowerCase().indexOf("-xx:heapdumppath=") + "-xx:heapdumppath=".length();
/*  99 */       int end = -1;
/* 100 */       for (int i = start; i < options.length(); i++) {
/* 101 */         char c = options.charAt(i);
/* 102 */         if (c == ' ') {
/* 103 */           end = i;
/* 104 */           break;
/*     */         }
/*     */       }
/* 107 */       String path = null;
/* 108 */       if (end > -1) {
/* 109 */         path = options.substring(start, end);
/*     */       } else {
/* 111 */         path = options.substring(start);
/*     */       }
/*     */       try {
/* 114 */         if (path.trim().length() <= 0) {
/* 115 */           throw new IOException("Heap dump path not specified correctly? mailboxd_java_options=" + LC.mailboxd_java_options.value());
/*     */         }
/* 117 */         File dir = new File(path);
/* 118 */         FileUtil.ensureDirExists(dir);
/* 119 */         if (!dir.canWrite()) {
/* 120 */           throw new IOException("Heap dump path not writable: " + path);
/*     */         }
/*     */       } catch (IOException e) {
/* 123 */         throw ServiceException.FAILURE("Unable to find/create HeapDumpPath", e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static void checkForClass(String clzName, String jarName) {
/*     */     try {
/* 130 */       String s = Class.forName(clzName).getName();
/* 131 */       ZimbraLog.misc.debug("checked for class " + s + " and found it");
/*     */     } catch (ClassNotFoundException cnfe) {
/* 133 */       ZimbraLog.misc.error(jarName + " not in your common/lib?", cnfe);
/*     */     } catch (UnsatisfiedLinkError ule) {
/* 135 */       ZimbraLog.misc.error("error in shared library used by " + jarName + "?", ule);
/*     */     }
/*     */   }
/*     */   
/*     */   private static String getSysProperty(String prop) {
/*     */     try {
/* 141 */       return System.getProperty(prop);
/*     */     } catch (SecurityException e) {}
/* 143 */     return "(accessing " + prop + " is not allowed by security manager)";
/*     */   }
/*     */   
/*     */   private static void logVersionAndSysInfo()
/*     */   {
/* 148 */     ZimbraLog.misc.info("version=" + BuildInfo.VERSION + " release=" + BuildInfo.RELEASE + " builddate=" + BuildInfo.DATE + " buildhost=" + BuildInfo.HOST);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 153 */     ZimbraLog.misc.info("LANG environment is set to: " + System.getenv("LANG"));
/*     */     
/* 155 */     ZimbraLog.misc.info("System property java.home=" + getSysProperty("java.home"));
/* 156 */     ZimbraLog.misc.info("System property java.runtime.version=" + getSysProperty("java.runtime.version"));
/* 157 */     ZimbraLog.misc.info("System property java.version=" + getSysProperty("java.version"));
/* 158 */     ZimbraLog.misc.info("System property java.vm.info=" + getSysProperty("java.vm.info"));
/* 159 */     ZimbraLog.misc.info("System property java.vm.name=" + getSysProperty("java.vm.name"));
/* 160 */     ZimbraLog.misc.info("System property java.vm.version=" + getSysProperty("java.vm.version"));
/* 161 */     ZimbraLog.misc.info("System property os.arch=" + getSysProperty("os.arch"));
/* 162 */     ZimbraLog.misc.info("System property os.name=" + getSysProperty("os.name"));
/* 163 */     ZimbraLog.misc.info("System property os.version=" + getSysProperty("os.version"));
/* 164 */     ZimbraLog.misc.info("System property sun.arch.data.model=" + getSysProperty("sun.arch.data.model"));
/* 165 */     ZimbraLog.misc.info("System property sun.cpu.endian=" + getSysProperty("sun.cpu.endian"));
/* 166 */     ZimbraLog.misc.info("System property sun.cpu.isalist=" + getSysProperty("sun.cpu.isalist"));
/* 167 */     ZimbraLog.misc.info("System property sun.os.patch.level=" + getSysProperty("sun.os.patch.level"));
/*     */   }
/*     */   
/*     */   private static void checkForClasses() {
/* 171 */     checkForClass("javax.activation.DataSource", "activation.jar");
/* 172 */     checkForClass("javax.mail.internet.MimeMessage", "javamail-1.4.3.jar");
/* 173 */     checkForClass("com.zimbra.znative.IO", "zimbra-native.jar");
/*     */   }
/*     */   
/*     */   public static void startup() {
/*     */     try {
/* 178 */       startup(true);
/*     */     } catch (ServiceException se) {
/* 180 */       halt("Exception during startup, aborting server, please check your config", se);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void startupCLI() throws ServiceException {
/* 185 */     startup(false);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private static synchronized void startup(boolean forMailboxd)
/*     */     throws ServiceException
/*     */   {
/* 194 */     if (sInited) {
/* 195 */       return;
/*     */     }
/* 197 */     sIsMailboxd = forMailboxd;
/* 198 */     if (sIsMailboxd) {
/* 199 */       FirstServlet.waitForInitialization();
/*     */     }
/*     */     
/* 202 */     Provisioning prov = Provisioning.getInstance();
/* 203 */     Server server = prov.getLocalServer();
/* 204 */     alwaysOnClusterId = server.getAlwaysOnClusterId();
/*     */     
/* 206 */     setSystemProperties();
/* 207 */     validateJavaOptions();
/*     */     
/* 209 */     logVersionAndSysInfo();
/*     */     
/* 211 */     SoapTransport.setDefaultUserAgent("ZCS", BuildInfo.VERSION);
/*     */     
/* 213 */     checkForClasses();
/*     */     
/* 215 */     ZimbraApplication app = ZimbraApplication.getInstance();
/*     */     
/* 217 */     DbPool.startup();
/*     */     
/* 219 */     app.initializeZimbraDb(forMailboxd);
/*     */     
/*     */ 
/* 222 */     if (!Versions.checkVersions()) {
/* 223 */       halt("Data version mismatch.  Reinitialize or upgrade the backend data store.");
/*     */     }
/*     */     
/* 226 */     DbPool.loadSettings();
/*     */     
/* 228 */     String tzFilePath = LC.timezone_file.value();
/*     */     try {
/* 230 */       File tzFile = new File(tzFilePath);
/* 231 */       WellKnownTimeZones.loadFromFile(tzFile);
/*     */     } catch (Throwable t) {
/* 233 */       halt("Unable to load timezones from " + tzFilePath, t);
/*     */     }
/*     */     
/* 236 */     if ((prov instanceof LdapProv)) {
/* 237 */       ((LdapProv)prov).waitForLdapServer();
/* 238 */       if (forMailboxd) {
/* 239 */         AttributeManager.loadLdapSchemaExtensionAttrs((LdapProv)prov);
/*     */       }
/*     */     }
/*     */     
/* 243 */     if (server.isMailSSLClientCertOCSPEnabled())
/*     */     {
/* 245 */       Security.setProperty("ocsp.enable", "true");
/*     */       
/* 247 */       System.setProperty("com.sun.security.enableCRLDP", "true");
/*     */     }
/*     */     else {
/* 250 */       Security.setProperty("ocsp.enable", "false");
/*     */       
/* 252 */       System.setProperty("com.sun.security.enableCRLDP", "false");
/*     */     }
/*     */     try
/*     */     {
/* 256 */       RightManager.getInstance();
/*     */     } catch (ServiceException e) {
/* 258 */       Util.halt("cannot initialize RightManager", e);
/*     */     }
/*     */     
/* 261 */     ZimbraHttpConnectionManager.startReaperThread();
/*     */     
/* 263 */     ExtensionUtil.initAll();
/*     */     try
/*     */     {
/* 266 */       StoreManager.getInstance().startup();
/*     */     } catch (IOException e) {
/* 268 */       throw ServiceException.FAILURE("Unable to initialize StoreManager.", e);
/*     */     }
/*     */     
/* 271 */     MailboxManager.getInstance();
/*     */     
/* 273 */     app.startup();
/*     */     
/* 275 */     if (app.supports(MemcachedConnector.class.getName())) {
/* 276 */       MemcachedConnector.startup();
/*     */     }
/* 278 */     if (app.supports(EhcacheManager.class.getName())) {
/* 279 */       EhcacheManager.getInstance().startup();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 284 */     MailboxIndex.startup();
/*     */     
/* 286 */     RedoLogProvider redoLog = RedoLogProvider.getInstance();
/* 287 */     if (sIsMailboxd) {
/* 288 */       redoLog.startup();
/*     */     } else {
/* 290 */       redoLog.initRedoLogManager();
/*     */     }
/*     */     
/* 293 */     System.setProperty("ical4j.unfolding.relaxed", "true");
/*     */     
/* 295 */     MailboxManager.getInstance().startup();
/*     */     
/* 297 */     app.initialize(sIsMailboxd);
/* 298 */     if (sIsMailboxd) {
/* 299 */       SessionCache.startup();
/* 300 */       AuthTokenRegistry.startup(prov.getConfig("zimbraAuthTokenNotificationInterval").getIntAttr("zimbraAuthTokenNotificationInterval", 60000));
/* 301 */       dbSessionCleanup();
/*     */       
/* 303 */       if (!redoLog.isSlave()) {
/* 304 */         boolean useDirectBuffers = server.isMailUseDirectBuffers();
/* 305 */         IoBuffer.setUseDirectBuffer(useDirectBuffers);
/* 306 */         ZimbraLog.misc.info("MINA setUseDirectBuffers(" + useDirectBuffers + ")");
/*     */         
/* 308 */         ServerManager.getInstance().startServers();
/*     */       }
/*     */       
/* 311 */       if (app.supports(WaitSetMgr.class.getName())) {
/* 312 */         WaitSetMgr.startup();
/*     */       }
/*     */       
/* 315 */       if (app.supports(MemoryStats.class.getName())) {
/* 316 */         MemoryStats.startup();
/*     */       }
/*     */       
/* 319 */       if (app.supports(ScheduledTaskManager.class.getName())) {
/* 320 */         ScheduledTaskManager.startup();
/*     */       }
/*     */       
/* 323 */       if (app.supports(PurgeThread.class.getName())) {
/* 324 */         PurgeThread.startup();
/*     */       }
/*     */       
/* 327 */       if (app.supports(AutoProvisionThread.class.getName())) {
/* 328 */         AutoProvisionThread.switchAutoProvThreadIfNecessary();
/*     */       }
/*     */       
/* 331 */       if (LC.smtp_to_lmtp_enabled.booleanValue()) {
/* 332 */         int smtpPort = LC.smtp_to_lmtp_port.intValue();
/* 333 */         int lmtpPort = Provisioning.getInstance().getLocalServer().getLmtpBindPort();
/* 334 */         SmtpToLmtp smtpServer = SmtpToLmtp.startup(smtpPort, "localhost", lmtpPort);
/* 335 */         smtpServer.setRecipientValidator(new SmtpRecipientValidator());
/*     */       }
/*     */       
/* 338 */       if (app.supports(AclPushTask.class)) {
/* 339 */         long pushInterval = server.getSharingUpdatePublishInterval();
/* 340 */         sTimer.schedule(new AclPushTask(), pushInterval, pushInterval);
/*     */       }
/*     */       
/* 343 */       if (app.supports(ExternalAccountManagerTask.class)) {
/* 344 */         long interval = server.getExternalAccountStatusCheckInterval();
/* 345 */         sTimer.schedule(new ExternalAccountManagerTask(), interval, interval);
/*     */       }
/*     */       
/* 348 */       if (prov.getLocalServer().isMessageChannelEnabled()) {
/*     */         try {
/* 350 */           MessageChannel.getInstance().startup();
/*     */         } catch (IOException e) {
/* 352 */           ZimbraLog.misc.warn("can't start notification channels", e);
/*     */         }
/*     */       }
/*     */       
/* 356 */       Server localServer = Provisioning.getInstance().getLocalServer();
/* 357 */       String provPort = localServer.getAttr("zimbraMailPort");
/* 358 */       String lcPort = LC.zimbra_mail_service_port.value();
/* 359 */       if (!lcPort.equals(provPort)) {
/*     */         try
/*     */         {
/* 362 */           LocalConfig lc = new LocalConfig(null);
/* 363 */           lc.set(LC.zimbra_mail_service_port.key(), provPort);
/* 364 */           lc.save();
/* 365 */           LC.reload();
/*     */         } catch (DocumentException|ConfigException|IOException e) {
/* 367 */           ZimbraLog.misc.warn("Cannot set LC zimbra_mail_service_port", e);
/*     */         }
/*     */       }
/*     */       
/*     */ 
/*     */ 
/* 373 */       if (app.supports(ZimbraPerf.class.getName())) {
/* 374 */         ZimbraPerf.initialize();
/*     */       }
/*     */     }
/*     */     
/* 378 */     ExtensionUtil.postInitAll();
/*     */     
/*     */ 
/* 381 */     if ((sIsMailboxd) && (isAlwaysOn())) {
/*     */       try {
/* 383 */         CuratorManager curatorManager = CuratorManager.getInstance();
/* 384 */         if (curatorManager == null) {
/* 385 */           throw ServiceException.FAILURE("ZooKeeper addresses not configured.", null);
/*     */         }
/* 387 */         curatorManager.start();
/*     */       } catch (Exception e) {
/* 389 */         throw ServiceException.FAILURE("Unable to start Distributed Lock service.", e);
/*     */       }
/*     */     }
/* 392 */     sInited = true;
/*     */   }
/*     */   
/*     */   public static synchronized void shutdown() throws ServiceException {
/* 396 */     if (!sInited) {
/* 397 */       return;
/*     */     }
/* 399 */     sInited = false;
/*     */     
/* 401 */     if (sIsMailboxd) {
/* 402 */       PurgeThread.shutdown();
/* 403 */       AutoProvisionThread.shutdown();
/*     */     }
/*     */     
/* 406 */     ZimbraApplication app = ZimbraApplication.getInstance();
/*     */     
/* 408 */     app.shutdown();
/*     */     
/* 410 */     if (sIsMailboxd) {
/* 411 */       if (app.supports(MemoryStats.class.getName())) {
/* 412 */         MemoryStats.shutdown();
/*     */       }
/*     */       
/* 415 */       if (app.supports(WaitSetMgr.class.getName())) {
/* 416 */         WaitSetMgr.shutdown();
/*     */       }
/*     */     }
/*     */     
/* 420 */     RedoLogProvider redoLog = RedoLogProvider.getInstance();
/* 421 */     if (sIsMailboxd) {
/* 422 */       if (!redoLog.isSlave()) {
/* 423 */         ServerManager.getInstance().stopServers();
/*     */       }
/*     */       
/* 426 */       dbSessionCleanup();
/*     */       
/* 428 */       SessionCache.shutdown();
/*     */       
/* 430 */       CuratorManager curatorManager = CuratorManager.getInstance();
/* 431 */       if (curatorManager != null) {
/* 432 */         curatorManager.stop();
/*     */       }
/*     */     }
/*     */     
/* 436 */     MailboxIndex.shutdown();
/*     */     
/* 438 */     if (sIsMailboxd) {
/* 439 */       redoLog.shutdown();
/*     */     }
/*     */     
/* 442 */     if (app.supports(ExtensionUtil.class.getName())) {
/* 443 */       ExtensionUtil.destroyAll();
/*     */     }
/*     */     
/* 446 */     if (app.supports(MemcachedConnector.class.getName())) {
/* 447 */       MemcachedConnector.shutdown();
/*     */     }
/* 449 */     if (app.supports(EhcacheManager.class.getName())) {
/* 450 */       EhcacheManager.getInstance().shutdown();
/*     */     }
/*     */     
/* 453 */     MailboxManager.getInstance().shutdown();
/*     */     
/* 455 */     if (sIsMailboxd) {
/* 456 */       StoreManager.getInstance().shutdown();
/*     */     }
/*     */     
/* 459 */     ZimbraHttpConnectionManager.shutdownReaperThread();
/*     */     
/* 461 */     sTimer.cancel();
/*     */     try
/*     */     {
/* 464 */       DbPool.shutdown();
/*     */     }
/*     */     catch (Exception ignored) {}
/*     */   }
/*     */   
/*     */   public static synchronized boolean started() {
/* 470 */     return sInited;
/*     */   }
/*     */   
/* 473 */   public static Timer sTimer = new Timer("Timer-Zimbra", true);
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void halt(String message)
/*     */   {
/*     */     try
/*     */     {
/* 482 */       ZimbraLog.system.fatal(message);
/*     */     } finally {
/* 484 */       Runtime.getRuntime().halt(1);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void halt(String message, Throwable t)
/*     */   {
/*     */     try
/*     */     {
/* 496 */       ZimbraLog.system.fatal(message, t);
/*     */     } finally {
/* 498 */       Runtime.getRuntime().halt(1);
/*     */     }
/*     */   }
/*     */   
/*     */   public static String getAlwaysOnClusterId() {
/* 503 */     return alwaysOnClusterId;
/*     */   }
/*     */   
/*     */   public static boolean isAlwaysOn() {
/* 507 */     return alwaysOnClusterId != null;
/*     */   }
/*     */   
/*     */   private static void dbSessionCleanup() throws ServiceException
/*     */   {
/* 512 */     DbPool.DbConnection conn = null;
/*     */     try {
/* 514 */       if (isAlwaysOn()) {
/* 515 */         conn = DbPool.getConnection();
/* 516 */         DbSession.deleteSessions(conn, Provisioning.getInstance().getLocalServer().getId());
/*     */         
/* 518 */         conn.commit();
/*     */       }
/*     */     } finally {
/* 521 */       if (conn != null) {
/* 522 */         conn.closeQuietly();
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/Zimbra.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */